<!--
 * @Description: 节点预警信息编辑
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2023-02-17 17:27:13
 * @LastEditTime: 2023-02-27 15:12:36
 * @FilePath: \cscec-pms-admin\src\views\construction\overdueNodeManagement\components\updateInfo.vue
-->
<template>
  <BaseModal :visible="visible" title="逾期节点管理" :width="900" destroy-on-close :wrap-class-name="'node-maintain'" :confirm-loading="submiting" @cancel="handleModalCancel()" @ok="handleSubmit">
    <custom-form ref="form" :is-validate="true" :edit-model="''" :form="form" :rules="rules" :form-list="updateFormOption" :is-emit="false">
      <a-date-picker slot="adjustCompletionTime" format="YYYY-MM-DD" :disabled-date="disabledDate" v-model="form.adjustCompletionTime" />
      <a-tree-select slot="typeId" v-model="form.typeId" placeholder="请选择" :tree-data="delayTypeTree" :replace-fields="replaceFields" show-search multiple  :filter-tree-node="filterTreeNode"  @change="changeName"/>
    </custom-form>
  </BaseModal>
</template>
<script>
import BaseModal from '@/components/BaseModal'
import overdueNodeManagement from '@/api/construction/overdueNodeManagement'
import weekPlanApi from '@/api/construction/weekPlanApi'

import { updateFormOption } from './../MasterPlan/model.js'

export default {
  name: 'weekPlanUpdateInfo',
  components: {
    BaseModal
  },
  props: {
    // 隐藏显示
    visible: {
      type: Boolean,
      require: true
    },
    // 模态框数据
    data: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      // 提交中
      submiting: false,
      //表单
      form: {},
      // 表单项
      // 逾期类别树
     delayTypeTree: [],
    // 逾期类别树字段替换
      replaceFields: {
        children: 'children',
        title: 'title',
        key: 'value',
        value: 'value'
      },
      typeId:null,
    }
  },
  computed: {
    updateFormOption(){
      return updateFormOption.filter(el=>el.keyName!=='periodDelayFileList')
    },
    // 校验
    rules() {
      const obj = {}
      this.updateFormOption.map(item => {
        if (item.rules) {
          obj[item.keyName] = item.rules
        }
      })
      return obj
    }
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.$checkRequireItem()
          this.form = this.$clone(this.data.list);
          let arr = []
          arr =  this.form.typeId &&  this.form.typeId.split(',');
          if(arr){
            this.form.typeId =  arr.map((item)=>item.trim());
          }
          this.typeId = this.data.list?.typeId;
          this.typeId = this.data.list?.typeId;
        } else {
          this.form = {}
        }
      }
    }
  },
  mounted(){
    this.getDelayType()
  },
  methods: {
    /**
     * @description: 下拉树形框搜索过滤
     * @param {string} input 输入值
     * @param {object} option 下拉对象
     * @return {boolean} 是否符合过滤条件
     */
     filterTreeNode(input, treeNode) {
      return treeNode.componentOptions.propsData.title.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
        /**
     * @description: 获取逾期类型
     * @param {void}
     * @return {void}
     */
     getDelayType() {
      weekPlanApi.labelTreeList({ pageNo: 1, pageSize: 1000, type: 11 }).then(({ code, data }) => {
        if (code === 200) {
          this.delayTypeTree = data
        }
      })
    },
    /**
     * @description: 禁用时间
     * @param {object} current 当前时间
     * @return {boolean}
     */
    disabledDate(current) {
      return current && current < this.moment(this.data.lastTime).endOf('day')
    },
    /**
     * @description: 关闭模态框
     * @param {boolean} 是否需要刷新
     * @return {void}
     */
    handleModalCancel(refresh = false) {
      this.$emit('update:visible', false)
      if (refresh) {
        this.$emit('refresh', true)
      }
    },
    changeName(name,data){
      this.typeId = name.join(", ");
      this.form.typeName =  data.join(", ");
    },
    /**
     * @description: 提交
     * @return {void}
     */
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submiting = true
          const { id, adjustCompletionTime, measure,typeName,periodDelayFileList} = this.form
          const params = {
            id,
            overdueNodeId: this.data.id,
            adjustCompletionTime: this.moment(adjustCompletionTime).format('YYYY-MM-DD'),
            measure,
            typeId:this.typeId,
            typeName,
            periodDelayFileList
          }
          overdueNodeManagement
            .overdueNodeCompletionUpdateOverdueNodeWarning(params)
            .then(res => {
              if (res.code === this.SUCCESS_CODE) {
                this.$message.success('编辑成功')
                this.handleModalCancel(true)
              }
            })
            .finally(() => {
              this.submiting = false
            })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped></style>
